const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const db = require('./model/db').connection

app.use((req, res, next) => {
    res.setHeader("Access-Control-Allow-Origin", "*");

    next()
})

app.use(bodyParser.urlencoded({ extended: true }))


//订单路由
app.get('/orderInfo', (req, res) => {
    db.query('select goodsID proID,customerID custID,money total,event state, employeeID num,orderDate date,  orderID,proAmount orderAmount from orders', (err, success) => {

        if (success) {
            res.send(success)
        } else {
            console.log(err);
        }
    })
})
app.post('/searchO', (req, res) => {
    db.query('select orderID,customerID custID,goodsID proID,proAmount orderAmount,employeeID num,orderDate date,event state,Money total from orders where orderID=?', [req.body.id], (err, success) => {
        if (success) {
            res.send(success)
        } else {
            console.log(err);
        }
    })
})
// 库存路由
app.get('/store', (req, res) => {

    db.query("select stock.goodsID as proID, stock.goodsName as proName, supplier.supplierName as supplier,  stockAmount as n from stock,supplier,goods where supplier.supplierID=goods.supplierID and stock.supplierID=supplier.supplierID  ", (err, success) => {
        if (success) {
            res.send(success)
        } else {
            console.log(err);
        }
    })
})
app.post('/searchStore', (req, res) => {
    db.query('select stock.goodsID as proID, stock.goodsName as proName, supplier.supplierName as supplier,  stockAmount as n from stock,supplier,goods where supplier.supplierID=goods.supplierID and stock.supplierID=supplier.supplierID  and stock.goodsID=?', [req.body.id], (err, success) => {
        if (success) {
            res.send(success)
        } else {
            console.log(err);
        }
    })
})
// 员工路由
app.get('/employee', (req, res) => {

    db.query("select hireDate date,employeeName pname,employeeSex sex,employeeAge age,duties role,salary money,employeeID eID from employee", (err, success) => {
        if (success) {
            res.send(success)
        }
    })
})
app.get('/employeeInfo', (req, res) => {
    db.query("select hireDate date,employeeName pname,employeeSex sex,employeeAge age,duties role,salary money,employeeID eID from employee where employeeID=?", [req.query.id], (err, success) => {
        if (success) {
            res.send(success)
        }
    })
})
app.post('/addEmployee', (req, res) => {
    db.query("insert into employee values(?,?,?,?,?,?,?)", [req.body.eID, req.body.date, req.body.pname, req.body.sex, req.body.age, req.body.role, req.body.money], (err, success) => {
        if (err) {
            console.log(err);
        }
    })
})
app.post('/deleteE', (req, res) => {
    db.query('delete from employee where employeeID=?', [req.body.id], (err, success) => {
        if (err) {
            console.log(err);
        }
    })
})
app.post('/editEmp', (req, res) => {
    db.query('update employee set hireDate = ?,employeeName=?,employeeSex=?,employeeAge=?,duties=?,salary=? where employeeID=?', [req.body.date, req.body.pname, req.body.sex, req.body.age, req.body.role, req.body.money, req.body.eID], (err, success) => {
        if (err) {
            console.log(err);
        }
    })
})
app.post('/searchEe',(req,res)=>{
    db.query("select hireDate date,employeeName pname,employeeSex sex,employeeAge age,duties role,salary money,employeeID eID from employee where employeeID=?", [req.body.id],(err,success)=>{
        if(success){
            res.send(success)
        }else{
            console.log(err);
        }
    })
})
// 顾客路由
app.get('/customer', (req, res) => {
    db.query("select customerID custID,customerName cname,customerAddress address,customerTel phone from customer", (err, success) => {
        if (success) {
            res.send(success)
        }
    })
})

app.get('/customerInfo', (req, res) => {
    db.query("select customerID custID,customerName cname,customerAddress address,customerTel phone from customer where customerID=?", [req.query.id], (err, success) => {
        if (success) {
            res.send(success[0])

        } else {
            console.log(err);
        }
    })
})
app.post('/deletC', (req, res) => {
    db.query('delete from customer where customerID=?', [req.body.id], (err, success) => {
        if (success) {
            console.log(1);
        }
    })
})
app.post('/addCustomer', (req, res) => {
    db.query('insert into customer values(?,?,?,?)', [req.body.custID, req.body.cname, req.body.phone, req.body.address], (err, success) => {
        if (err) {
            console.log(err);
        }
    })
})
app.post('/searchC', (req, res) => {
    db.query("call select_customer(?)", [req.body.id], (err, success) => {
        if (success) {
            res.send(success)
        } else {
            console.log(err);
        }
    })
})
app.post('/editCust', (req, res) => {
    db.query('update customer set customerName=?,customerTel=?,customerAddress=? where customerID=?', [req.body.cname, req.body.phone, req.body.address,req.body.custID], (err, success) => {
        if (err) {
            console.log(err);
        }
    })
})
// 供应商路由
app.get('/supplier', (req, res) => {
    db.query("select supplierID sID,supplierName sName,supplierTel phone from supplier", (err, success) => {
        if (success) {
            res.send(success)
        }
    })
})
app.get('/supplierInfo', (req, res) => {

    db.query('select supplierID sID,supplierName sName,supplierTel phone from supplier where supplierID=?', [req.query.id], (err, success) => {
        if (success) {
            res.send(success[0])
        } else {
            console.log(err);
        }
    })
})
app.post('/addSupplier', (req, res) => {
    db.query('insert into supplier values(?,?,?)', [req.body.id, req.body.name, req.body.phone], (err, success) => {
        if (err) {
            console.log(err);
        }
    })
})
app.post('/deleteS', (req, res) => {
    db.query("delete from supplier where supplierID=?", [req.body.id], (err, success) => {
        if (err) {
            console.log(err);
        }
    })
})
app.post('/searchS', (req, res) => {
    db.query("call select_supplier(?)", [req.body.id], (err, success) => {
        if (success) {
            res.send(success)
        } else {
            console.log(err);
        }
    })
})
app.post('/editS', (req, res) => {
    db.query('update  supplier set supplierName=? ,supplierTel=? where supplierID=?', [req.body.name, req.body.phone, req.body.id], (err, success) => {
        if (err) {
            console.log(err);
        }
    })
})
// 矿泉水路由
app.get('/product', (req, res) => {

    db.query("select goodsID proID,goodsName proName,supplierID supplier,purchasePrice inPrice,salePrice outPrice from goods ", (err, success) => {
        if (success) {
            res.send(success)
        }
    })
})
app.get('/productInfo', (req, res) => {
    db.query('select goodsID proID,goodsName proName,supplierID supplier,purchasePrice inPrice,salePrice outPrice from goods where goodsID=?', [req.query.id], (err, success) => {
        if (success) {
            res.send(success)
        }
    })
})

app.post('/addProduct', (req, res) => {
    db.query('insert into goods value(?,?,?,?,?)', [req.body.proID, req.body.proName, req.body.supplier, req.body.inPrice, req.body.outPrice], (err, success) => {
        if (err) {
            console.log(err);
        }
    })
})
app.post('/deleteP', (req, res) => {
    db.query('delete from goods where goodsID=?', [req.body.id], (err, success) => {
        if (err) {
            console.log(err);
        }
    })
})
app.post('/searchP', (req, res) => {
    db.query('call select_goods(?)', [req.body.id], (err, success) => {
        if (success) {
            res.send(success)
        } else {
            console.log(err);
        }
    })
})
app.post('/editPro', (req, res) => {
    db.query('update goods set goodsName=?,supplierID=?,purchasePrice=?,salePrice=? where goodsID=?', [req.body.proName, req.body.supplier, req.body.inPrice, req.body.outPrice, req.body.proID], (err, success) => {
        if (err) {
            console.log(err);
        }
    })
})


// 进货路由
app.get('/buy', (req, res) => {
    db.query('select * from purchase', (err, success) => {
        if (success) {
            res.send(success)
        }
    })
})
app.get('/getOptions', (req, res) => {
    db.query('select goodsID from goods', (err, success) => {
        if (success) {
            res.send(success)
        }
    })
})
app.get('/getEmployeeID', (req, res) => {
    db.query("select employeeID from employee", (err, success) => {
        if (success) {
            res.send(success)
        }
    })
})

// 新增订单路由
app.get('/getPro', (req, res) => {
    db.query("select goodsID from goods", (err, success) => {
        if (success) {
            res.send(success)
        }
    })
})
app.get('/getEmp', (req, res) => {
    db.query("select employeeID from employee", (err, success) => {
        if (success) {
            res.send(success)
        }
    })
})
app.post('/addOrder', (req, res) => {
    db.query("INSERT INTO orders SET customerID=?,goodsID=?,proAmount=?,employeeID=?,orderDate=?,event=?,money=proAmount*(SELECT salePrice FROM goods WHERE goodsID=? LIMIT 1)",
        [parseInt(req.body.name), parseInt(req.body.proID), parseInt(req.body.num), parseInt(req.body.employeeID), req.body.date,req.body.state, parseInt(req.body.proID)],
        (err, success) => {
            if (err) {
                console.log(err);
            }
        })
})

app.listen(3000)
console.log('服务器启动成功');